package us.gaaoc.framework.webapp.backingbeans.clerk;

import java.util.Date;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.constants.Constants;

import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;
import us.gaaoc.framework.webapp.util.Messages;

/**
 * 
 * @author hea
 * @date Apr 28, 2006
 * @version: GAJE 1.5
 * 
 */
@SuppressWarnings("unchecked")
public class RejectConfirmationPage extends BaseBackingBean {

	/**
	 * Properties
	 */
	
	/** Generated Serial Version UID */
	private static final long serialVersionUID = 4384620264445872403L;
	
	private String rejectionReason;

	private UIInput rejectionReasonInput;

	private FacesMessage message; 
	
	
	
	
	/**
	 * Constructor
	 *
	 */
	public RejectConfirmationPage(){
		
	}

	/**
	 * Getter for property rejectionReason
	 * @return rejetionReason
	 */
	public String getRejectionReason() {
		return rejectionReason;
	}

	/**
	 * Setter for property rejectionReason
	 * @param rejectionReason
	 */
	public void setRejectionReason(String rejectionReason) {
		this.rejectionReason = rejectionReason;
	}

	/**
	 * Getter for property rejectionReasonInput
	 * @return rejectionReasonInput
	 */
	public UIInput getRejectionReasonInput() {
		return rejectionReasonInput;
	}

	/**
	 * Setter for property rejectionReasonInut
	 * @param rejectionReasonInput
	 */
	public void setRejectionReasonInput(UIInput rejectionReasonInput) {
		this.rejectionReasonInput = rejectionReasonInput;
	}
	
	/**
	 * This method resets the rejection reason input field
	 * @return String
	 */
	public String reset(){
		this.getRejectionReasonInput().setSubmittedValue(null);
		return "stay";
	}
	
	/**
	 * This mathod navigates user to the brevious page
	 * @return String
	 */
	public String back(){		
		return "gotoCaseDetailsPage";
	}

	/**
	 * This method gets called when the clerk confirm to reject
	 * the case record
	 * @return String
	 */
	public void rejectFiling() {
		Map sessionMap = this.getFacesContext().getExternalContext().getSessionMap();		
		String caseUuid = (String) sessionMap.get("caseUuid");
		String courtUuid = (String) sessionMap.get("courtUuid");
				
		this.getModelFacade().updateRejectedCaseRecord(
				caseUuid, 
				this.getRejectionReason(), 
				Constants.CaseRecordStatus.REJECTED, 
				courtUuid);
		
		Person person = (Person) this.getFacesContext().getExternalContext().getSessionMap().get("person");

		this.getModelFacade().addCaseRecordHistory(
				caseUuid,
				Constants.CaseRecordEventType.FILING_REJECTED,
				person, 
				courtUuid,
				"no comments");		
			
		
	}
	
	/**
	 * This method validates the rejected reason typed by user
	 * @param context
	 * @param component
	 * @param value
	 * @throws ValidatorException
	 * @throws Exception
	 */
	public void validateReason(FacesContext context, UIComponent component,
			Object value) throws ValidatorException, Exception {
		String rejectionReason = (String) value;
		if (!isValidReason(rejectionReason)) {
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);
		}
	}

	/**
	 * Helper method to validate the rejection reason
	 * @param value
	 * @return
	 * @throws Exception
	 */
	protected boolean isValidReason(String value) throws Exception {
		boolean isValid = true;
		char[] charArray = value.toCharArray();
		
		if(value.length() > 100) // 100 character limit is hard-coded in the database schema
		{
			isValid = false;
			message = Messages.getMessage(
					"us.gaaoc.framework.webapp.util.errorMessage",
					"tooLargeRejectionReason", new Object[] { value.length()-100 });
			message.setDetail(message.getDetail());
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
		}else{
			for (char c : charArray) {
				if (Character.isDigit(c) || Character.isLetter(c)
						|| Character.isSpaceChar(c)) {
				} else {
					String s = new Character(c).toString();
					if (s.equals("!") || s.equals(",") || s.equals(";")
							|| s.equals(".") || s.equals("'") || s.equals("\"")
							|| s.equals("%") || s.equals("(") || s.equals(")") || s.equals(":") 
							|| ((Integer.valueOf((int)c)) == 13)  // carriage return
							|| ((Integer.valueOf((int)c)) == 10)) // newline		
					{} else {							
						isValid = false;
						message = Messages.getMessage(
								"us.gaaoc.framework.webapp.util.errorMessage",
								"incorrectRejectionReason", new Object[] { value, s });
						message.setDetail(message.getDetail());
						message.setSeverity(FacesMessage.SEVERITY_ERROR);
						break;
					}
				}
			}
		}
		return isValid;
	}

	

}